package icbc.app.control
{
	import com.greensock.TweenLite;
	import com.greensock.easing.Strong;
	
	import flash.display.DisplayObject;
	import flash.display.Loader;
	import flash.display.LoaderInfo;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.IOErrorEvent;
	import flash.net.URLRequest;
	
	import icbc.app.manager.ExplodeFxManager;
	import icbc.app.util.Utils;
	import icbc.app.util.logger.Log;
	import seg.core.event.GameEvent;
	import seg.core.model.style.BaseStyle;
	
	public class Score extends Sprite
	{
		public static const STAR_SHIP_IMAGE_PATH:String = "style/images/score_starship_maze.png";
		private static const TOTAL_POINTS:uint = 6;
		private var starImagePath:String = "style/images/score_star_maze.png";
		private var controlWidth:uint;
		private var controlHeight:uint;
		private var points:Sprite;
		private var starship:Sprite;
		private var total:uint = TOTAL_POINTS;
		private var _imageloading:Number = 0;
		private var shipx:uint = 0;
		private var shipy:uint = 0;
		private var shiptween:TweenLite;
		private var style:BaseStyle;
		
		public function Score(style:BaseStyle, controlWidth:uint = 300, controlHeight:uint = 80)
		{
			super();
			this.style = style;
			this.controlWidth = controlWidth;
			this.controlHeight = controlHeight;
			x = 580;
			y = 508;
			starImagePath = style.scoreStar;
			addEventListener(Event.ADDED_TO_STAGE, initStage);
		}
		
		private function initStage(evt:Event):void
		{
			removeEventListener(Event.ADDED_TO_STAGE, initStage);
			drawBackground();

			points = new Sprite();
			addChild(points);

			starship = new Sprite();
			addChild(starship);

			buildPoints();
			buildStarship();
		}

		private function drawBackground():void
		{
			graphics.beginFill(0x000000,0);
			graphics.drawRoundRect(0,0, controlWidth, controlHeight, 40, 40);
			graphics.endFill();
		}
		
		private function buildStarship():void
		{
			 starship.addChild(loadImage(STAR_SHIP_IMAGE_PATH));
		}

		private function buildPoints():void
		{
			if(points != null)
			{
				points.removeChildren();
			}
			for(var i:uint = 1; i <= total; i++)
			{
				points.addChild(buildPoint(i));
			}
			points.x = 95;
			points.y = 20;
		}
		
		private function buildPoint(idx:uint):DisplayObject
		{
			var gap:uint = 33;
			var star:DisplayObject = loadImage(starImagePath);
			star.name = idx.toString();
			star.x = gap*(idx-1);
			return star;
		}
		
		private function loadImage(path:String):DisplayObject
		{
			_imageloading++;
			var loader:Loader = new Loader();
			loader.contentLoaderInfo.addEventListener(Event.COMPLETE, imageComplete);
			loader.contentLoaderInfo.addEventListener(IOErrorEvent.IO_ERROR, onIOError);
			loader.load(new URLRequest(path));
			return loader;
		}
		
		private function onIOError(evt:IOErrorEvent):void
		{
			trace("Avatar.loadImage.IOError: "+ evt.text);
			removeLoaderListener(evt);
		}		
		
		private function imageComplete(evt:Event):void
		{
			_imageloading--;
			removeLoaderListener(evt);
			if(_imageloading==0)
			{
				//
			}
		}
		
		private function removeLoaderListener(evt:Event):void
		{
			var loader:LoaderInfo = evt.target as LoaderInfo;
			loader.removeEventListener(Event.COMPLETE, imageComplete);
			loader.removeEventListener(IOErrorEvent.IO_ERROR, onIOError);
		}

		
		public function removePoint():void
		{
			dropPoint(points.getChildAt(--total));
			if(total == 0)
			{
				dispatchEvent(new Event(GameEvent.LEVELS_FAILED));
			}
		}
		
		public function isFailed():Boolean
		{
			return total == 0;
		}
		
		public function reset():void
		{
			total = TOTAL_POINTS;
			buildPoints();
		}
		
		private function dropPoint(star:DisplayObject):void
		{
			if(shiptween != null && shiptween.active)
			{
				exploteStar(star);
				return;
			};
			var posx:uint = points.x + star.x - 70;
			shiptween = new TweenLite(starship, 1, {
				x:posx,
				ease:Strong.easeInOut,
				onComplete:function():void
				{
					exploteStar(star)
					shiptween.reverse();
				}
			});
		}
		
		private function exploteStar(star:DisplayObject):void
		{
			var explodeManager:ExplodeFxManager = new ExplodeFxManager();
			var explode:Sprite;
			explode = explodeManager.getStarExplode(star);
			points.addChild(explode);
			points.removeChild(star);
			explode.x = star.x;
			explode.y = star.y;
		}
	}
}